package cn.edu.hqu.cst.android.ls.aihelper6;

import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.speech.RecognizerIntent;

import android.speech.tts.TextToSpeech;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.google.gson.Gson;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import interfaces.heweather.com.interfacesmodule.bean.Lang;
import interfaces.heweather.com.interfacesmodule.bean.Unit;
import interfaces.heweather.com.interfacesmodule.bean.air.now.AirNow;
import interfaces.heweather.com.interfacesmodule.bean.weather.now.Now;
import interfaces.heweather.com.interfacesmodule.view.HeConfig;
import interfaces.heweather.com.interfacesmodule.view.HeWeather;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import android.os.Bundle;

public class Voice extends AppCompatActivity {
    //申请录音权限
    private static final int GET_RECODE_AUDIO = 1;
    private static String[] PERMISSION_AUDIO = { Manifest.permission.RECORD_AUDIO };
    private final int SPEECH_RECOGNITION_CODE = 1;
    private ImageButton button;
    private TextToSpeech textToSpeech = null;//创建自带语音对象
    LinearLayout add;
    //声明定位回调监听器
    private  String CityId;
    String answer;
    //初始化定位
    public AMapLocationClient mLocationClient = null;
    public AMapLocationClientOption mLocationOption=null;
    String detail;
    StringBuilder Show;
    String[] a = null;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            AddChat((String) msg.obj,1);
            startAuto((String) msg.obj);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_voice);
        judgePermission();
        button = findViewById(R.id.btn_img);
        SpeechUtility.createUtility(this, SpeechConstant.APPID +"=\n" + "5ec79b44");
        HeConfig.init("HE2005182304151563","cd075d4f3e4a4720b1b44ce354cfda61");
        HeConfig.switchToFreeServerNode();
        initTTS();
        initMap();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOnline()) {
                    textToSpeech.stop();//停止之前正在读的语音
                    //StartSpeechToText();//开始读新的语音
                    initSpeech(Voice.this);
                } else {
                    Toast.makeText(Voice.this, "尚未联网！", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }

    //判断是否联网
    protected boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        } else {
            return false;
        }
    }

    //问答
    private void answer_question(String question){
        answer = "很抱歉没听清，再说一次看看？";
        if(question.contains("华侨大学")){
            //问题 1 ：
            if (question.contains("校庆日")) {
                answer = "华侨大学的校庆日是11月1号。";
            }
            //问题 2 ：
            if (question.contains("几个") & question.contains("校区")) {
                answer = "华侨大学一共有两个校区，分别是泉州校区和厦门校区。";
            }
            //问题 3 && 4 ：
            if ((question.contains("厦门") | question.contains("泉州")) & (question.contains("地址") | question.contains("在哪里"))) {
                if(question.contains("厦门")){
                    answer = "华侨大学厦门校区地址为：福建省厦门市集美大道668号。";
                }
                if(question.contains("泉州")){
                    answer = "华侨大学泉州校区地址为：福建省泉州市丰泽区城华北路269号。";
                }
            }
            //问题 5 ：
            if (question.contains("校长")) {
                answer = "华侨大学的校长是徐西鹏先生。";
            }
            //问题 6 ：
            if (question.contains("部门") & question.contains("管辖")) {
                answer = "华侨大学隶属中央统战部管辖。";
            }
            //问题 7 ：
            if (question.contains("校训")) {
                answer = "华侨大学的校训是：会通中外，并育德才。";
            }
            //问题 8 ：
            if (question.contains("校歌")) {
                answer = "华侨大学的校歌是：《一方沃土》。";
            }
            //问题9
            if (question.contains( "历史" ) | question.contains( "创办" ) | question.contains( "时候" ) | question.contains( "时间" )) {
                answer = "华侨大学创办于1960年，距今已有60年的历史，2020年11月1日，华侨大学将迎来60周年校庆，祝华侨大学生日快乐！";
            }
            //问题10
            if (question.contains( "211" ) | question.contains( "985" )) {
                answer = "华侨大学不是985院校，也不是211院校" ;
            }
            //问题11
            if (question.contains( "双一流" )) {
                answer = "华侨大学是福建省双一流建设高校" ;
            }
            //问题12
            if (question.contains( "学院" )) {
                answer = "华侨大学有28个教学学院，分别是：·国际学院·哲学与社会发展学院·经济与金融学院·法学院·国际关系学院/新侨学院·马克思主义学院/通识教育学院·体育学院·文学院·华文学院·外国语学院·新闻与传播学院·数学科学学院·统计学院·机电及自动化学院·材料科学与工程学院·信息科学与工程学院·计算机科学与技术学院·土木工程学院·化工学院·建筑学院·工学院·医学院/生物医学学院工商管理学院·政治与公共管理学院·旅游学院·音乐舞蹈学院·美术学院·厦航学院/泛华学院·美国中文学院·南洋学院·继续教育学院·创新创业学院" ;
            }
            //问题13
            if (question.contains( "住" ) | question.contains( "宿舍" ) | question.contains( "人间" )) {
                answer = "华侨大学宿舍以4人间为主，也有2人间与6人间，境外生、研究生多住2人间，境内生本科生、研究生新生多住4人间，本科生新生多住6人间" ;
            }
            //问题14
            if (question.contains( "吃" ) | question.contains( "食堂" ) | question.contains( "餐厅" )) {
                answer = "华侨大学泉州校区拥有第一餐厅、第二餐厅、第三餐厅、清真主题餐厅、西苑餐厅、南苑餐厅6家食堂；厦门校区拥有凤林餐厅、凤翔餐厅、凤竹餐厅、凤华餐厅、紫荆一餐厅、紫荆二餐厅、紫荆三餐厅7家食堂；菜色鲜香，品种丰富，菜系综合，符合大多学生的口味" ;
            }
            //问题15
            if (question.contains( "周边" )) {
                answer = "华侨大学厦门校区周边有:万科云城、银泰百货、万达广场等商业设施；泉州校区周边有泰禾广场、宝德城东商城、中骏世界城等商业设施；龙舟池校区周边有石鼓路等商业设施" ;
            }
            //问题16
            if (question.contains( "设施" ) | question.contains( "有图书馆" ) | question.contains( "有田径场" ) | question.contains( "有篮球场" ) | question.contains( "有羽网馆" ) | question.contains( "有游泳池" )) {
                answer = "华侨大学拥有图书馆、田径场、篮球场、实验室、羽网馆、游泳池等设施" ;
            }
            //问题17
            if (question.contains( "去" ) | question.contains( "走" ) | question.contains( "到" ) | question.contains( "公交" )) {
                answer = "华侨大学厦门校区乘坐BRT快1线、快6线可达，2023年即将开通地铁6号线，公交491路;691路;900路;902路;906路;907路;915路;948路;949路;955路;992路可达；泉州校区乘坐公交11路;11路复线;19路;35路;42路;203路;K502路;K502路夜班;K605路;K701路;K702路长线;K702路短线可达；龙舟池校区乘公交901路;905路;923路;935路;961路;集游1线可达" ;
            }
            //问题18
            if (question.contains( "男篮" ) | question.contains( "CUBA" ) | question.contains( "篮球" ) | question.contains( "冠军" )) {
                answer = "华侨大学是CUBA九冠王，是2018年世界大学生3V3篮球赛男篮冠军" ;
            }
            //问题19
            if (question.contains( "骑" ) | (question.contains( "校内" ) && question.contains( "公交" ))) {
                answer = "允许骑自行车上学，不允许骑电动车、摩托车上学，学校有校园公交可供搭乘" ;
            }
            //问题20
            if (question.contains( "电话" ) | question.contains( "联系" )) {
                answer = "华侨大学招生处热线0595-22695678" ;
            }

            AddChat(question+"?",0);
            AddChat(answer,1);
            startAuto(answer);
        }
        else if(question.contains("天气")){
            AddChat(question+"?",0);
            getWether();
        }
        else{
            AddChat(question+"?",0);
            TuringAns(question,handler);
        }

    }

    //添加对话框
    public void AddChat(String a,int s) {
        TextView textView = new TextView(this);
        textView.setPadding(10, 10, 10, 10);
        if(s != 0){textView.setBackgroundResource(R.drawable.text);}
        textView.setText(a);
        textView.setTextSize(12);
        textView.setGravity(View.TEXT_ALIGNMENT_CENTER);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        params.setMargins(30, 15, 30, 15);
        add = findViewById(R.id.main);
        add.addView(textView,params);
    }

    /*
             语音模块开始
     */
    private void initTTS() {
        //实例化自带语音对象
        textToSpeech = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int status) {
                if (status == textToSpeech.SUCCESS) {
                    //判断是否支持下面两种语言
                    int result1 = textToSpeech.setLanguage(Locale.US);
                    int result2 = textToSpeech.setLanguage(Locale.SIMPLIFIED_CHINESE);
                    boolean a = (result1 == TextToSpeech.LANG_MISSING_DATA || result1 == TextToSpeech.LANG_NOT_SUPPORTED);
                    boolean b = (result2 == TextToSpeech.LANG_MISSING_DATA || result2 == TextToSpeech.LANG_NOT_SUPPORTED);
                } else {
                    Toast.makeText(Voice.this, "数据丢失或不支持", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //发声
    private void startAuto(String data) {
        // 设置音调，值越大声音越尖（女生），值越小则变成男声,1.0是常规
        textToSpeech.setPitch(1.0f);
        // 设置语速
        textToSpeech.setSpeechRate(1.0f);
        textToSpeech.speak(data,//输入中文，若不支持的设备则不会读出来
                TextToSpeech.QUEUE_FLUSH, null);
    }

    private void StartSpeechToText() {
        Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
        intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
        try {
            startActivityForResult(intent, SPEECH_RECOGNITION_CODE); // 1
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this, "对不起，没听清！", Toast.LENGTH_SHORT).show();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case SPEECH_RECOGNITION_CODE: {
                if (resultCode == RESULT_OK && null != data) {
                    //获取语音数据
                    ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); //String
                    //AddChat(result.toString());
                    answer_question(result.get(0));
                }
                break;
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        textToSpeech.stop(); // 不管是否正在朗读TTS都被打断
        textToSpeech.shutdown(); // 关闭，释放资源
    }
    /*
             语音模块结束
     */

    /*
             获取定位
     */
    private void initMap() {
        //初始化定位
        mLocationClient=new AMapLocationClient(Voice.this);
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        mLocationOption = new AMapLocationClientOption();
//设置定位模式为高精度模式，AMapLocationMode.Battery_Saving为低功耗模式，AMapLocationMode.Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        mLocationOption.setNeedAddress(true);//设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setOnceLocation(false);//设置是否只定位一次,默认为false
        mLocationOption.setWifiActiveScan(true);//设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setMockEnable(false);//设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setInterval(15000);//设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setOnceLocation(false);//可选，是否设置单次定位默认为false即持续定位
        mLocationOption.setOnceLocationLatest(false); //可选，设置是否等待wifi刷新，默认为false.如果设置为true,会自动变为单次定位，持续定位时不要使用
        mLocationOption.setWifiScan(true); //可选，设置是否开启wifi扫描。默认为true，如果设置为false会同时停止主动刷新，停止以后完全依赖于系统刷新，定位位置可能存在误差
        mLocationOption.setLocationCacheEnable(true); //可选，设置是否使用缓存定位，默认为true
//给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
//启动定位
        mLocationClient.startLocation();
    }

    protected void TuringAns(String a, final Handler handler){
        String APIKEY = "be2f963e0f05416b95a2d0044f867da6";
        String INFO = null;
        try {
            INFO = URLEncoder.encode(a, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        final String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder().url(getURL).build();
                try {
                    Response response = okHttpClient.newCall(request).execute();
                    //返回城市列表json数据，信息列表
                    String responseData = response.body().string();
                    JSONObject jsonAns = null;
                    try {
                        jsonAns = new JSONObject(responseData);
                        String jsonAnswer = jsonAns.getString("text");
                        System.out.println("变成json数据的格式：" + jsonAnswer);
                        Message message = new Message();
                        message.obj =jsonAnswer;
                        handler.sendMessage(message);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public AMapLocationListener mLocationListener = new AMapLocationListener(){
        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    //定位成功回调信息，设置相关消息
                    aMapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                    aMapLocation.getAccuracy();//获取精度信息
                    //  aMapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
                    //  aMapLocation.getCountry();//国家信息
                    //  aMapLocation.getProvince();//省信息
                    //  aMapLocation.getCity();//城市信息
                    //   aMapLocation.getDistrict();//城区信息
                    detail = aMapLocation.getDistrict()+aMapLocation.getStreet();
                    //aMapLocation.getStreet();//街道信息
                    // = aMapLocation.getCityCode();//城市编码
                    //     aMapLocation.getAdCode();//地区编码
                    //获取经纬度
                    double  LongitudeId = aMapLocation.getLongitude();
                    double LatitudeId = aMapLocation.getLatitude();
                    //获取定位城市定位的ID
                    requestCityInfo(LongitudeId,LatitudeId);
                    mLocationClient.stopLocation();//停止定位
                } else {
                    //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                    Log.e("info", "location Error, ErrCode:"
                            + aMapLocation.getErrorCode() + ", errInfo:"
                            + aMapLocation.getErrorInfo());
                }
            }
        }
    };
    public void  requestCityInfo(double longitude,double latitude){
        //这里的key是webapi key
        String cityUrl = "https://search.heweather.net/find?location="+longitude+","+latitude+"&key=b794e0b223a7455fb609db10f0b93ed6";
        sendRequestWithOkHttp(cityUrl);
    }
    //解析根据经纬度获取到的含有城市id的json数据，这里我使用原始的jsonObject与jsonArray来解析，网上也有使用jsonformat插件来，你们也可以那样做
    private void sendRequestWithOkHttp(final String cityUrl) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder().url(cityUrl).build();
                try {
                    Response response = okHttpClient.newCall(request).execute();
                    //返回城市列表json数据，信息列表
                    String responseData = response.body().string();
                    System.out.println("变成json数据的格式："+responseData);
                    JSONObject jsonWeather = null;
                    try {
                        jsonWeather = new JSONObject(responseData);
                        //返回的信息列表
                        JSONArray jsonArray = jsonWeather.getJSONArray("HeWeather6");
                        JSONObject jsonObject = jsonArray.getJSONObject(0);
                        String jsonStatus = jsonObject.getString("status");
                        System.out.println("解析以后的内容："+jsonStatus);
                        if (jsonStatus.equals("ok")){
                            //获取所在城市的id
                            JSONArray jsonBasic = jsonObject.getJSONArray("basic");
                            JSONObject jsonCityId = jsonBasic.getJSONObject(0);
                            CityId = jsonCityId.getString("cid");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /*
                 天气查询
     */
    private void getWether() {
        /**
         * 实况天气
         * 实况天气即为当前时间点的天气状况以及温湿风压等气象指数，具体包含的数据：体感温度、
         * 实测温度、天气状况、风力、风速、风向、相对湿度、大气压强、降水量、能见度等。
         * @param context  上下文
         * @param location 地址详解
         * @param lang       多语言，默认为简体中文
         * @param unit        单位选择，公制（m）或英制（i），默认为公制单位
         * @param listener  网络访问回调接口
         */
        HeWeather.getWeatherNow(Voice.this, CityId, Lang.CHINESE_SIMPLIFIED, Unit.METRIC, new HeWeather.OnResultWeatherNowBeanListener() {
            public static final String TAG = "he_feng_now";
            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError: ", e);
                System.out.println("Weather Now Error:" + new Gson());
            }
            @Override
            public void onSuccess(Now dataObject) {
                Log.i(TAG, " Weather Now onSuccess: " + new Gson().toJson(dataObject));
                String jsonData = new Gson().toJson(dataObject);
                String weather = null, temperature = null,time = null;
                StringBuilder loc = new StringBuilder("");
                if (dataObject.getStatus().equals("ok")) {
                    String JsonNow = new Gson().toJson(dataObject.getNow());
                    String JsonLoc = new Gson().toJson(dataObject.getBasic());
                    String JsonTime = new Gson().toJson(dataObject.getUpdate());
                    JSONObject jsonObject = null;
                    JSONObject jsonGetBasic = null;
                    JSONObject jsonGetUpdate = null;
                    try {
                        jsonObject = new JSONObject(JsonNow);
                        jsonGetBasic = new JSONObject(JsonLoc);
                        jsonGetUpdate = new JSONObject(JsonTime);
                        weather = jsonObject.getString("cond_txt");
                        temperature = jsonObject.getString("tmp");
                        time = jsonGetUpdate.getString("loc");
                        loc.append("当前位于"+jsonGetBasic.getString("admin_area")+"省"+jsonGetBasic.getString("parent_city")+"市");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(Voice.this, "有错误", Toast.LENGTH_SHORT).show();
                    return;
                }
                StringBuilder Show = new StringBuilder("");
                String wendu2 = temperature + "℃";
                Show.append(loc+detail+","+"当前天气："+ weather+","+"当前温度：" + wendu2+","+"更新于："+time);
                AddChat(String.valueOf(Show),1);
                startAuto(String.valueOf(Show));
            }
        });
    }
    /**
     * 初始化语音识别
     */
    public void initSpeech(final Context context) {
        //1.创建RecognizerDialog对象
        RecognizerDialog mDialog = new RecognizerDialog(context, null);
        //2.设置accent、language等参数
        mDialog.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
        mDialog.setParameter(SpeechConstant.ACCENT, "mandarin");
        //3.设置回调接口
        mDialog.setListener(new RecognizerDialogListener() {
            @Override
            public void onResult(RecognizerResult recognizerResult, boolean isLast) {
                if (!isLast) {
                    //解析语音
                    //返回的result为识别后的汉字,直接赋值到TextView上即可
                    String result = parseVoice(recognizerResult.getResultString());
                    answer_question(result);
                }
            }

            @Override
            public void onError(SpeechError speechError) {

            }
        });
        //4.显示dialog，接收语音输入
        mDialog.show();
    }

    /**
     * 解析语音json
     */
    public String parseVoice(String resultString) {
        Gson gson = new Gson();
        Voice1 voiceBean = gson.fromJson(resultString, Voice1.class);

        StringBuffer sb = new StringBuffer();
        ArrayList<Voice1.WSBean> ws = voiceBean.ws;
        for (Voice1.WSBean wsBean : ws) {
            String word = wsBean.cw.get(0).w;
            sb.append(word);
        }
        return sb.toString();
    }
    /**
     * 语音对象封装
     */
    public class Voice1 {

        public ArrayList<WSBean> ws;

        public class WSBean {
            public ArrayList<CWBean> cw;
        }

        public class CWBean {
            public String w;
        }
    }
    //6.0之后要动态获取权限，重要！！！
    protected void judgePermission() {
        int permission = ActivityCompat.checkSelfPermission(Voice.this,Manifest.permission.RECORD_AUDIO);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(Voice.this, PERMISSION_AUDIO,GET_RECODE_AUDIO); }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // 检查该权限是否已经获取
            // 权限是否已经 授权 GRANTED---授权  DINIED---拒绝

            // sd卡权限
            String[] SdCardPermission = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (ContextCompat.checkSelfPermission(this, SdCardPermission[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, SdCardPermission, 100);
            }

            //手机状态权限
            String[] readPhoneStatePermission = {Manifest.permission.READ_PHONE_STATE};
            if (ContextCompat.checkSelfPermission(this, readPhoneStatePermission[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, readPhoneStatePermission, 200);
            }

            //定位权限
            String[] locationPermission = {Manifest.permission.ACCESS_FINE_LOCATION};
            if (ContextCompat.checkSelfPermission(this, locationPermission[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, locationPermission, 300);
            }

            String[] ACCESS_COARSE_LOCATION = {Manifest.permission.ACCESS_COARSE_LOCATION};
            if (ContextCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, ACCESS_COARSE_LOCATION, 400);
            }

            String[] READ_EXTERNAL_STORAGE = {Manifest.permission.READ_EXTERNAL_STORAGE};
            if (ContextCompat.checkSelfPermission(this, READ_EXTERNAL_STORAGE[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, READ_EXTERNAL_STORAGE, 500);
            }

            String[] WRITE_EXTERNAL_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
            if (ContextCompat.checkSelfPermission(this, WRITE_EXTERNAL_STORAGE[0]) != PackageManager.PERMISSION_GRANTED) {
                // 如果没有授予该权限，就去提示用户请求
                ActivityCompat.requestPermissions(this, WRITE_EXTERNAL_STORAGE, 600);
            }

        }else{
            //doSdCardResult();
        }
        //LocationClient.reStart();
    }
}
